package br.com.guardaroupavirtual.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

import br.com.guardaroupavirtual.facade.Facade;
import br.com.guardaroupavirtual.helper.Diretorios;
import br.com.guardaroupavirtual.helper.HibernateHelper;
import br.com.guardaroupavirtual.model.Categoria;
import br.com.guardaroupavirtual.model.LookEvento;
import br.com.guardaroupavirtual.model.Produto;

import com.google.gson.Gson;

/**
 * Servlet implementation class HomeController
 */
@WebServlet("/HomeController")
public class HomeController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private Facade fachada;
	private Session session;


	private String cmd = null;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		cmd = request.getParameter("cmd");
		
		if (cmd == null || cmd.isEmpty()) {	
			request.getRequestDispatcher(String.format(Diretorios.Home, "index.jsp")).forward(request, response);
		}
		else{
			if (cmd.equalsIgnoreCase("GetCategorias")) {
				this.getCategorias(request, response);				
			}
			if(cmd.equalsIgnoreCase("GetProdutosByFiltro"))
			{			
				this.getProdutosByFiltro(request, response);
			}
			if(cmd.equalsIgnoreCase("GetLookEventos"))
			{			
				this.getLookEventos(request, response);
			}			
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	private void getCategorias(HttpServletRequest request, HttpServletResponse response){
		try {
			PrintWriter out = response.getWriter();			
			Gson object = new Gson();
			response.setContentType("application/json");
			session = HibernateHelper.getSessionFactory().openSession();
			fachada = Facade.getCurrentInstance();
			List<Categoria> categorias = fachada.getCategorias(session);			
			out.write(object.toJson(categorias));
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	private void getLookEventos(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			PrintWriter out = response.getWriter();			
			Gson object = new Gson();
			response.setContentType("application/json");
			session = HibernateHelper.getSessionFactory().openSession();
			fachada = Facade.getCurrentInstance();
			List<LookEvento> looks = fachada.getLooksEvento(session);			
			out.write(object.toJson(looks));
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}	
	
	private void getProdutosByFiltro(HttpServletRequest request, HttpServletResponse response)
	{		
		try {
			PrintWriter out = response.getWriter();			
			Gson object = new Gson();
			response.setContentType("application/json");
			
			session = HibernateHelper.getSessionFactory().openSession();
			fachada = Facade.getCurrentInstance();
			
			Map<String, String> parametros = new HashMap<String, String>();
			
			parametros.put("sexo", request.getParameter("sexo"));
			parametros.put("medida", request.getParameter("medida"));
			
			parametros.put("categoriaID", request.getParameter("categoriaID"));
			parametros.put("lookEventoID", request.getParameter("lookEventoID"));
			
			List<Produto> produtos = fachada.getProdutosByFiltro(session, parametros);			
			out.write(object.toJson(produtos));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	

}
